package com.doyutu.seed.utils;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.stats.CacheStats;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

/**
 * @author DoyuTu
 * @version 0.0.1
 * spring-boot-project-seed
 * Caffeine实现LRU CACHE util
 */
public class CacheUtil {

    private static final Cache<Object, Object> CACHE = Caffeine.newBuilder()
            .expireAfterWrite(20, TimeUnit.MINUTES)
            .expireAfterAccess(15,TimeUnit.MINUTES)
            .maximumSize(1000)
            .softValues()
            .recordStats()
            .build();

    private CacheUtil() {
    }

    public Object getValue(Object key) {
        return CACHE.getIfPresent(key);
    }

    public void putValue(Object key, Object value) {
        CACHE.put(key, value);
    }

    public void removeValue(Object key) {
        CACHE.invalidate(key);
    }

    public void removeAll() {
        CACHE.cleanUp();
    }

    public ConcurrentMap<Object, Object> asMap() {
        return CACHE.asMap();
    }

    public CacheStats getStats() {
        return CACHE.stats();
    }

    public long getSize() {
        return CACHE.estimatedSize();
    }
}
